#include "Window.h"
/*-----------------------------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
/*-----------------------------------------------------------------------------------------------*/
Window::Window(HINSTANCE hInstance) : m_hInstance(hInstance)
{

}
/*-----------------------------------------------------------------------------------------------*/
ATOM Window::Register()
{
   WNDCLASS wc;

   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
   wc.hCursor = LoadCursor( NULL, IDC_ARROW );
   wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
   wc.hInstance = m_hInstance;
   wc.lpfnWndProc = WndProc;
   wc.lpszClassName = TEXT( "EngineWindow" );
   wc.style = CS_HREDRAW | CS_VREDRAW;
   wc.lpszMenuName = TEXT( "Engine" );

   return RegisterClass(&wc); 
}
/*-----------------------------------------------------------------------------------------------*/
bool Window::Create(const int width, const int height)
{
   if ( Register() == 0 )
      return false;

   m_width = width;
   m_height = height;

   m_hwnd = CreateWindow(
      TEXT("EngineWindow"),
      TEXT("Engine"),
      WS_OVERLAPPEDWINDOW,
      10,
      10,
      m_width,
      m_height,
      NULL,
      NULL,
      m_hInstance,
      NULL
      );

   ShowWindow( m_hwnd, SW_SHOW );

   UpdateWindow( m_hwnd );

   return true;
}
/*-----------------------------------------------------------------------------------------------*/
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
   switch( message )
   {
      case WM_DESTROY:
      case WM_QUIT:
         PostQuitMessage(0);
      break;
      
      case WM_KEYDOWN:
      {
         switch (wparam)
         {
            case VK_ESCAPE:
               PostQuitMessage(0);
            break;
            case VK_SPACE:
               std::cout << "WOPA!" << std::endl;
            break;
         }
         break;
      }
      
      default:
         return DefWindowProc( hwnd, message, wparam, lparam );
   }

   return 0;
}
/*-----------------------------------------------------------------------------------------------*/
HWND Window::GetHwnd()
{
   return m_hwnd;
}
/*-----------------------------------------------------------------------------------------------*/
HINSTANCE Window::GetHInstance()
{
   return m_hInstance;
}
/*-----------------------------------------------------------------------------------------------*/
void Window::SetTitle(const std::string & title)
{
   SetWindowText( m_hwnd, title.c_str() );
}
